package nancal.mp.web.open

import nancal.mp.client.integration.TestFeignClient
import nancal.mp.db.es.entity.NginxLogIndex
import nancal.mp.db.es.esr
import nancal.mp.db.mongo.SubSystemEnum
import nancal.mp.db.mongo.entity.admin.*
import nancal.mp.db.mongo.entity.sys.*
import nancal.mp.db.mongo.mor
import nbcp.base.annotation.*
import nbcp.base.annotation.log.MyLogLevel
import nbcp.base.annotation.mvc.*
import nbcp.base.comm.ApiResult
import nbcp.base.db.IdName
import nbcp.base.enums.LogLevelScopeEnum
import nbcp.base.extend.*
import nbcp.base.utils.Md5Util
import nbcp.mvc.sys.ClientIp
import nbcp.myoql.db.es.component.query
import nbcp.myoql.db.mongo.query
import nbcp.myoql.db.mongo.updateWithEntity
import org.slf4j.LoggerFactory
import org.springframework.beans.factory.annotation.Autowired
import org.springframework.web.bind.annotation.RequestMapping
import org.springframework.web.bind.annotation.RequestMethod
import org.springframework.web.bind.annotation.RestController
import springfox.documentation.annotations.ApiIgnore
import java.time.LocalDateTime
import javax.servlet.http.HttpServletRequest

/**
 * Created by udi on 17-5-23.
 */

@OpenAction
@RestController
@RequestMapping
class Open_Test_Controller {
    companion object {
        private val logger = LoggerFactory.getLogger(this::class.java.declaringClass)
    }

    @Autowired
    lateinit var testClient: TestFeignClient

    @MyLogLevel(LogLevelScopeEnum.DEBUG)
    @RequestMapping("/hi2", method = arrayOf(RequestMethod.GET, RequestMethod.POST))
    fun hi(name: String): String {
        return testClient.dep().ToJson()
    }


    @ApiIgnore
    @RequestMapping("/init", method = arrayOf(RequestMethod.GET))
    fun init(request: HttpServletRequest, session: HttpServletRequest): String {
        var admin = AdminUser();
        if (mor.admin.adminUser.query()
                .where { it.loginName mongoEquals "admin" }
                .exists() == false
        ) {
            admin.loginName = "admin";
            admin.name = "管理员"
            admin.isAdmin = true;

            mor.admin.adminUser.doInsert(admin);
        }

        if (mor.admin.adminLoginUser.query()
                .where { it.loginName mongoEquals "admin" }
                .exists() == false
        ) {
            var admin_login = AdminLoginUser();
            admin_login.id = admin.id;
            admin_login.loginName = "admin"
            admin_login.password = Md5Util.getBase64Md5("1234");

            mor.admin.adminLoginUser.doInsert(admin_login);
        }

        var corp = nancal.mp.db.mongo.entity.Corporation();
        if (mor.corp.corporation.query()
                .where { it.name mongoEquals "admin" }
                .exists() == false
        ) {
            corp.name = "admin"

            mor.corp.corporation.doInsert(corp);
        }

        var user = nancal.mp.db.mongo.entity.CorpUser();
        if (mor.corp.corpUser.query()
                .where { it.loginName mongoEquals "admin" }
                .exists() == false
        ) {
            user.loginName = "admin"
            user.isAdmin = true
            user.corp = IdName(corp.id, corp.name)

            mor.corp.corpUser.doInsert(user);
        }


        if (mor.corp.corpLoginUser.query()
                .where { it.loginName mongoEquals "admin" }
                .exists() == false
        ) {
            var loginUser = nancal.mp.db.mongo.entity.CorpLoginUser();
            loginUser.id = user.id
            loginUser.loginName = "admin"
            loginUser.password = Md5Util.getBase64Md5("1234")

            mor.corp.corpLoginUser.doInsert(loginUser);
        }

        return "OK";
    }

    @ApiIgnore
    @RequestMapping("/init-ldap", method = arrayOf(RequestMethod.GET))
    fun init_ldap(request: HttpServletRequest, session: HttpServletRequest): String {

        mor.sys.subSystemInfo.updateWithEntity(
            SubSystemInfo(
                SubSystemEnum.gitea,
                "/dev/gitea.html?",
                "/user/login",
                "/api/v1/admin/users?access_token=863e994112b022b0943ee10cef6acb978f357a92",
                "user_name=@loginName&password=@password",
                "31004",
                302,
                "administrator",
                "1@#4Qwer",
                ""
            )
        ).whereColumn { it.name }.execSave()


        mor.sys.subSystemInfo.updateWithEntity(
            SubSystemInfo(
                SubSystemEnum.jenkins,
                "/dev/jenkins.html?",
                "/j_acegi_security_check",
                "/securityRealm/createAccountByAdmin",
                "j_username=@loginName&j_password=@password",
                "8081",
                302,
                "admin",
                "123456987",
                "11a369d5ec308d34e21414cbc799246ef0"
            )
        ).whereColumn { it.name }.execSave()


        mor.sys.subSystemInfo.updateWithEntity(
            SubSystemInfo(
                SubSystemEnum.harbor,
                "/dev/harbor.html?",
                "/c/login",
                "/api/v2.0/users",
                "7003",
                "principal=@loginName&password=@password",
                200,
                "admin",
                "Nancal1234",
                ""
            )
        ).whereColumn { it.name }.execSave()

/*        mor.sys.subSystemInfo.updateWithEntity(
            SubSystemInfo(SubSystemEnum.microService, "", "",
                    "", 200, "", "", "")
        ).updateOrAdd()*/

/*        mor.sys.subSystemInfo.updateWithEntity(
            SubSystemInfo(SubSystemEnum.skywalking, "", "",
                    "", 200, "", "", "")
        ).updateOrAdd()*/

        mor.sys.subSystemInfo.updateWithEntity(
            SubSystemInfo(
                SubSystemEnum.yapi,
                "/dev/yapi.html?",
                "/api/user/login",
                "/api/plugin/fine/user/add",
                "31010",
                "email=@loginName&password=@password",
                200,
                "sunyd@nancal.com",
                "123456",
                ""
            )
        ).whereColumn { it.name }.execSave()

        mor.sys.subSystemInfo.updateWithEntity(
            SubSystemInfo(
                SubSystemEnum.grafana,
                "/dev/grafana.html?",
                "/login",
                "/api/admin/users",
                "8302",
                "user=@loginName&password=@password",
                200,
                "admin",
                "admin",
                ""
            )
        ).whereColumn { it.name }.execSave()

        mor.sys.subSystemInfo.updateWithEntity(
            SubSystemInfo(
                SubSystemEnum.sonarqube,
                "/dev/sonarqube.html?",
                "/api/authentication/login",
                "/api/users/create",
                "31006",
                "login=@loginName&password=@password",
                200,
                "admin",
                "admin",
                ""
            )
        ).whereColumn { it.name }.execSave()

        mor.sys.subSystemInfo.updateWithEntity(
            SubSystemInfo(
                SubSystemEnum.kuboard,
                "/dev/kuboard.html?",
                "/dashboard?k8sToken=",
                "",
                "8301",
                "",
                200,
                "",
                "",
                ""
            )
        ).whereColumn { it.name }.execSave()

        if (mor.sys.subSystemUserInfo.query()
                .where { it.user.name mongoEquals "admin" }
                .exists() == false
        ) {
            var user = mor.corp.corpUser.query().where { it.loginName mongoEquals "admin" }.toEntity()!!

            var subUser = SubSystemUserInfo();
            subUser.user = IdName(user.id, "admin")
            subUser.authList = mutableListOf(
                UserAuthInfo(SubSystemEnum.gitea, "administrator", "1@#4Qwer"),
                UserAuthInfo(SubSystemEnum.confluence, "admin", "fw101"),
                UserAuthInfo(SubSystemEnum.jenkins, "admin", "123456987"),
                UserAuthInfo(SubSystemEnum.harbor, "admin", "Nancal1234"),
//                UserAuthInfo(SubSystemEnum.microService, "", ""),
//                UserAuthInfo(SubSystemEnum.skywalking, "", ""),
                UserAuthInfo(SubSystemEnum.yapi, "sunyd@nancal.com", "123456"),
                UserAuthInfo(SubSystemEnum.grafana, "admin", "admin"),
                UserAuthInfo(SubSystemEnum.sonarqube, "admin", "admin"),
                UserAuthInfo(
                    SubSystemEnum.kuboard,
                    "",
                    "eyJhbGciOiJSUzI1NiIsImtpZCI6ImpuSV9adzFCYkRZcVNUM2ZqTm9Mc1BMcEZtT0lvVDVXd2pkcnpfRWFkZVUifQ.eyJpc3MiOiJrdWJlcm5ldGVzL3NlcnZpY2VhY2NvdW50Iiwia3ViZXJuZXRlcy5pby9zZXJ2aWNlYWNjb3VudC9uYW1lc3BhY2UiOiJrdWJlLXN5c3RlbSIsImt1YmVybmV0ZXMuaW8vc2VydmljZWFjY291bnQvc2VjcmV0Lm5hbWUiOiJrdWJvYXJkLXVzZXItdG9rZW4taGprMmQiLCJrdWJlcm5ldGVzLmlvL3NlcnZpY2VhY2NvdW50L3NlcnZpY2UtYWNjb3VudC5uYW1lIjoia3Vib2FyZC11c2VyIiwia3ViZXJuZXRlcy5pby9zZXJ2aWNlYWNjb3VudC9zZXJ2aWNlLWFjY291bnQudWlkIjoiMzkyZGFhN2UtMmNhYS00MDBhLWFkOWItZmIzZGQ1YzY4ZWYxIiwic3ViIjoic3lzdGVtOnNlcnZpY2VhY2NvdW50Omt1YmUtc3lzdGVtOmt1Ym9hcmQtdXNlciJ9.mWxpi3YLQnfRkrc2_v12Awjsc1IRjVPISCLxuLk6N4f23v3wfN1K543GGUmHo2yJZwuqzqW7uV362kLXySi3L2f50HXvOFXkirEdxM6XU_qMwsfwzW9kXkCRhsJ7C6p7qzCVehPPmMgmcnyhXkx5cL85O10If2Wq3M17Fu6WhK5kgLsQJME0dp3gySWOcR1oFjhEYJGTCoyxzRNy4kXhV35WjvqfOdwrkJn9YUjFbOjW-QphuTI14nif82kuA8ekbVn-d-LJGxNycHcFY3HFKsECjz3DVwyauP7pg_vFv5gkMrLyZZi-ZiF_WjCVXuPX-0QJ7DRvdk7WQvAQd5d1UQ"
                )
            )

            mor.sys.subSystemUserInfo.doInsert(subUser);
        }

        return "OK"
    }
}
